package com.xiong.selenium.model;

import com.xiong.selenium.edge.EdgeModel;
import com.xiong.selenium.model.ext.WebDriverCallBack;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;

public class WebDriverModel {




    private WebDriver webDriver;

    /**
     * 打开网址
     * @param url
     * @return
     */
    public WebDriverModel openUrl(String url){
        this.webDriver.get(url);
        return this;
    }

    /**
     * 窗口最大化
     * @return
     */
    public WebDriverModel windowFull(){
        this.webDriver.manage().window().maximize();
        return this;
    }

    /**
     * 休眠
     * @param time
     * @return
     */
    public WebDriverModel sleep(long time){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 输入通过id
     * @param id id
     * @param inputContent 输入值
     * @return
     */
    public WebDriverModel inputElementById(String id,String inputContent){
        // 定位输入框元素
        WebElement input = this.webDriver.findElement(By.id(id));
        // 在输入框中输入文本
        input.sendKeys(inputContent);
        return this;
    }

    /**
     * 点击回车键
     * @return
     */
    public WebDriverModel clickEnter(){
        Actions actions = new Actions(this.webDriver);
        actions.sendKeys(Keys.ENTER).perform();
        return this;
    }

    /**
     * 点击通过id
     * @return
     */
    public WebDriverModel clickElementById(String id){
        WebElement element = this.webDriver.findElement(By.id(id));
        // 点击元素
        element.click();
        return this;
    }

    /**
     * 双击某个元素通过id
     * @param id
     * @return
     */
    public WebDriverModel doubleClickElementById(String id){
        WebElement element = this.webDriver.findElement(By.id(id));
        // 创建 Actions 类实例
        Actions actions = new Actions(this.webDriver);
        // 双击元素
        actions.doubleClick(element).perform();
        return this;
    }

    /**
     * 将光标移动到某个元素上
     * @param id
     * @return
     */
    public WebDriverModel cursorMoveElementById(String id){
        WebElement element = this.webDriver.findElement(By.id(id));
        // 创建 Actions 类实例
        Actions actions = new Actions(this.webDriver);

        actions.moveToElement(element).perform();
        return this;
    }

    /**
     * 处理自定义webdriver
     * @param webDriverCallBack
     * @return
     */
    public WebDriverModel dealOther(WebDriverCallBack webDriverCallBack){
        webDriverCallBack.dealWebDriver(this.webDriver);
        return this;
    }

    /**
     * 获取节点下的网页代码通过id
     * @param id
     * @return
     */
    public String getElementHtmlCodeById(String id){
        WebElement element = this.webDriver.findElement(By.id(id));
        String outerHtml = element.getAttribute("outerHTML");
        return outerHtml;
    }


    public String getElementHtmlCode(){
        WebElement element=this.webDriver.findElement(By.tagName("body"));
        String outerHtml = element.getAttribute("outerHTML");
        return outerHtml;
    }

    /**
     * 添加cookie
     * @param cookieName
     * @param cookieValue
     * @return
     */
    public WebDriverModel addCookie(String cookieName,String cookieValue){
        Cookie customCookie = new Cookie(cookieName, cookieValue);
        this.webDriver.manage().addCookie(customCookie);
        return this;
    }

    /**
     * 刷新页面
     */
    public void refresh(){
        this.webDriver.navigate().refresh();
    }


    public WebDriverModel setLocalStorageItem(String key,String value){
        this.executeJs("localStorage.setItem('"+key+"', '"+value+"');");
        return this;
    }


    /**
     * 退出测试
     */
    public void quit(){
        this.webDriver.quit();
    }

    /**
     * 执行js
     * @param js
     * @return
     */
    public Object executeJs(String js){
        JavascriptExecutor javascriptExecutor=(JavascriptExecutor) webDriver;
        Object val=javascriptExecutor.executeScript(js);
        return val;
    }

    /**
     * 屏幕向下滑动
     * @param num
     */
    public void windowScrollTo(int num){
        int x=0;
        int height=100;
        while (x<num){
            height+=height;
            executeJs("window.scrollTo(100,"+height+");");
            x++;
        }
    }


    public void windowScrollToBottom(){
        // 设置滚动的初始位置
        long scrollHeight = 0;

        // 滑动到屏幕底部
        while (true) {
            // 执行 JavaScript 代码来滑动到底部
            executeJs("window.scrollTo(0, document.body.scrollHeight);");
            // 等待一段时间，使页面加载完成
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 获取滚动后的页面高度
            long newScrollHeight = (long) executeJs("return document.body.scrollHeight;");

            // 如果滚动后的页面高度没有变化，说明已经滑动到底部，结束循环
            if (newScrollHeight == scrollHeight) {
                break;
            }
            // 更新滚动的初始位置，继续循环滑动
            scrollHeight = newScrollHeight;
        }

    }

    /**
     * 获取web驱动
     * @return
     */
    public WebDriver getWebDriver(){
        return this.webDriver;
    }

    public void setWebDriver(WebDriver webDriver){
        this.webDriver=webDriver;
    }
}
